package com.ce.source.analysis.queue;


import java.util.Iterator;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * @author ：xiaopingzhang8@creditease.cn
 * @Description ：双向队列deque分析
 * @ClassName ：BlockDequeAnalyse
 * @Company ：普信恒业科技发展（北京）有限公司
 * @date ：2017/11/28 14:49
 */
public class BlockDequeAnalyse {
    public static void main(String[] args) {
        analysePut();
    }

    /**
     * put方法分析
     * <p>
     *     <ol>
     *         <li>初始化LinkedBlockingDeque的容量，默认Integer.MAX_VALUE</li>
     *         <li>初始化Node节点</li>
     *         <li>获得ReentrantLock（可重入锁）</li>
     *         <li>插入节点<br />
     *             <div>
     *                  Node l = last
     *                  node.prev=last
     *                  last = node
     *                  l.next = last
     *             </div>
     *         </li>
     *         <li>
     *             如果数据长度大于等于容量count >= capacity,进入等待
     *         </li>
     *     </ol>
     * </p>
     */
    public static void analysePut(){
        LinkedBlockingDeque<Object> deque = new LinkedBlockingDeque<Object>(4);
        try {
            deque.put(1);
            deque.put(2);
            deque.put(3);
            deque.put(4);
//            deque.put(5);
            traverse(deque);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


    /**
     * 遍历
     */
    public static void traverse(BlockingDeque<Object> deque){
        Iterator<Object> it = deque.iterator();
        while (it.hasNext()){
            System.out.print(it.next()+"\t");
        }

    }

}
